package edu.sctu.practice.class02.hujie.my_practice.day20151119collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * Created by hp on 2015/11/24.
 */
public class SetTest {
    public List<Course> coursesToSelect;
    private Scanner console;
    public Student student;

    public SetTest() {
        coursesToSelect = new ArrayList<Course>();
        console = new Scanner(System.in);
    }

    public void testAdd() {
        /**
         * 创建一个课程对象，并通过调用add方法，
         * 添加到备选课程list中
         * */
        Course cr1 = new Course("1", "数据结构");
        coursesToSelect.add(cr1);
        Course temp = (Course) coursesToSelect.get(0);
//        System.out.println("添加了课程:" + temp.id + ":" + temp.name);
        //第二种添加到制定位置
        Course cr2 = new Course("2", "C语言");
        coursesToSelect.add(0, cr2);
        Course temp2 = (Course) coursesToSelect.get(0);
//        System.out.println("添加了课程:" + temp2.id + ":" + temp2.name);

        Course[] courses = {new Course("3", "离散数学"), new Course("4", "汇编语言")};
        coursesToSelect.addAll(Arrays.asList(courses));
        Course temp3 = (Course) coursesToSelect.get(2);
        Course temp4 = (Course) coursesToSelect.get(3);
//        System.out.println("添加了两门课程:" + temp3.id + ":" + temp3.name + ":"
//                + temp4.id + ":" + temp4.name);
        Course[] courses2 = {new Course("5", "高等数学"), new Course("6", "大学英语")};
        coursesToSelect.addAll(2, Arrays.asList(courses2));
        Course temp5 = (Course) coursesToSelect.get(2);
        Course temp6 = (Course) coursesToSelect.get(3);
//        System.out.println("添加了两门课程:" + temp5.id + ":" + temp5.name + ":"
//                + temp6.id + ":" + temp6.name);

    }

    /**
     * 通过for each 方法访问集合元素
     * 和上面的一样的
     */
    public void testForEach() {
        for (Object obj : coursesToSelect) {
            Course cr = (Course) obj;
            System.out.println("课程：" + cr.id + ":" + cr.name);
        }
    }

    /**
     * 测试list的contains方法:是否包含某个
     */
    public void testListContians() {
        //取得备选课程序列的第0个元素
        Course course = coursesToSelect.get(0);
        //打印输出coursesToSelect是否包含course对象
        System.out.println("取得课程：" + course.name);
        System.out.println("取得课程:" + course.name +
                "," + coursesToSelect.contains(course));
        //提示输入课程名称
        System.out.println("请输入课程名：");
        String name = console.next();
        //创建一个新的课程对象，ID和名称，与course对象一样
        Course course2 = new Course();
        course2.name = name;
        System.out.println("新创建课程：" + course2.name);
        System.out.println("备选课程中是否包含课程：" + course2.name
                + "," + coursesToSelect.contains(course2));
//通过indexOf方法来获取某元素的索引位置
        if (coursesToSelect.contains(course2))
            System.out.println("课程：" + course2.name +
                    "的索引位置为：" + coursesToSelect.indexOf(course2));
    }

    //创建学生对象并选课
    public void createStudentAndSelectCourse() {
//创建一个学生对象
        student = new Student("1", "小明");
        System.out.println("欢迎学生：" + student.name + "选课！");
        //创建一个scanner对象，用来接收从键盘输入的课程ID
        Scanner console = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入课程ID");
            String courseID = console.next();
            for (Course cr : coursesToSelect) {
                if (cr.id.equals(courseID)) {
                    student.course.add(cr);
                    /**
                     * Set中，添加某个对象，无论添加多少次，
                     * 最终只会保留一个该对象（的引用）
                     * 并且，保留的是第一次添加的那一个
                     * 无序存放
                     * */
//                    student.course.add(null);
//                    student.course.add(cr);
                }
            }
        }
        //st.testForEachForSet(student);
    }

    public void testSetContains() {
        //提示输入课程名称
        System.out.println("请输入学生已选的课程名称：");
        System.out.println("请输入课程名：");
        String name = console.next();
        //创建一个新的课程对象，ID和名称，与course对象一样
        Course course2 = new Course();
        course2.name = name;
        System.out.println("新创建课程：" + course2.name);
        System.out.println("备选课程中是否包含课程：" + course2.name
                + "," + student.course.contains(course2));

    }


    public static void main(String[] args) {
        SetTest st = new SetTest();
        st.testAdd();
        st.testListContians();
        st.testForEach();
//      st.createStudentAndSelectCourse();
//      st.testSetContains();
//        st.testForEachForSet(student);

    }

    public void testForEachForSet(Student student) {
        //打印输出学生所选择的课程
        System.out.println("共选择了：" + student.course.size() + "课程！");
        for (Course cr : student.course) {
            System.out.println("选择了课程：" + cr.id + ":" + cr.name);

        }
    }
}

